<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8">
	<title>Dijit manager unit test</title>
	<style type="text/css">
		@import "../../themes/claro/document.css";
		@import "../css/dijitTests.css";
	</style>
	<script type="text/javascript" src="../../../dojo/dojo.js"
		data-dojo-config="isDebug: true"></script>
	<script type="text/javascript" src="../_testCommon.js"></script>

	<script type="text/javascript">
		dojo.require("doh.runner");
		dojo.require("dojo.parser");
		dojo.require("dijit.dijit");

		dojo.addOnLoad(function(){
			dojo.declare("foo", dijit._Widget, {
				name: "",
				attr1: 0,
				attr2: 0
			});
	
			dojo.declare("bar", dijit._Widget, {
				name: "",
				attr1: 0,
				attr2: 0
			});
	
			dojo.declare("Baz", dijit._Widget, {
				name: "",
				attr1: 1,
				attr2: 1
			});

			doh.register("parse", function(){
				dojo.parser.parse();
			});

			doh.register("dijit._base.manager",
				[
					function forEachTest(t){
						var names=[];
						dijit.registry.forEach(function(widget){ names.push(widget.name); });
						t.is(names.join(" "), "bob is your uncle");
					},
					function filterTest(t){
						var names=[];
						dijit.registry.
							filter(function(widget){ return widget.attr1==10; }).
							forEach(function(widget){ names.push(widget.name); });
						t.is(names.join(" "), "bob uncle");
					},
					function filterScope(t){
						// testing filter with a forced 'this' in the cb function
						var names = [],
							obj = {
								check: function(widget, val){
									// same as previous unit test
									return widget.attr1 == val;
								}
							};
						dijit.registry
							.filter(function(w){ return this.check(w, 10); }, obj)
							.forEach(function(w){ names.push(w.name) })
						;
						t.is(names.join(" "), "bob uncle");
					},
					function byId(t){
						t.is(dijit.byId("three").name, "your");
						t.f(dijit.byId("nonexistant"));
					},
					function byClass(t){
						var names=[];
						dijit.registry.
							byClass("bar").
							forEach(function(widget){ names.push(widget.name); });
						t.is(names.join(" "), "your uncle");
					},
					function lengthTest(t){
						// test existing length
						t.is(dijit.registry.byClass("bar").length, 2);
						// test adding:
						var n = new Baz();
						t.is(dijit.registry.byClass("Baz").length, 1);
						// test destroying:
						n.destroy();
						t.is(dijit.registry.byClass("Baz").length, 0);
					},
					function nullLengthTest(t){
						var ws = new dijit.WidgetSet();
						var count = 0;

						var b = [new Baz(), new Baz()];
						ws.add(b[0]);
						ws.add(b[1]);

						t.is(2, ws.length);

						ws.remove(b[0].id);
						t.is(1, ws.length);

						ws.remove(b[1].id);
						t.is(0, ws.length);

						// ensure we don't ever go below 0
						ws.remove("invalidId");
						t.is(0, ws.length);

						dojo.forEach(b, function(w){ w.destroy() });

					},
					function forEachScope(t){
						var obj = {
							getIt: function(widget){
								return widget.id;
							}
						};

						dijit.registry.forEach(function(w){
							t.is(w.id, this.getIt(w));
						}, obj);
					},
					function forEachReturn(t){
						// also tests WidgetSet.map
						var ws = dijit.registry.forEach(function(){}).map(function(w){
							return w.declaredClass.toUpperCase();
						});
						t.t(dojo.isArray(ws));
						t.is(ws.length, dijit.registry.length);
					},
					function forEachInc(t){
						var x = 0;
						dijit.registry.forEach(function(w, i){
							t.is(i, x++);
						});
					},
					function toArrayTest(t){
						var ws = dijit.registry.byClass("bar");
						var wa = ws.toArray();

						t.t(dojo.isArray(wa));
						t.is(ws.length, wa.length);

						var wda = dojo.map(wa, function(w){
							return w.domNode;
						});

						t.is(wda.length, ws.length);
						var wdas = wda.slice(-1);

						t.is(1, wdas.length);

						var w = dijit.byNode(wdas[0]);
						t.is(w.declaredClass, "bar");
					},
					function mapTest(t){
						var ids = dijit.registry.map(function(w){
							return w.id;
						});
						t.is(ids.length, dijit.registry.length);
						dijit.registry.forEach(function(id){
							t.t(dijit.byId(id));
						});
					},
					function everyTest(t){

						var a = new Baz();
						var b = new Baz();

						var hasid = dijit.registry.every(function(w){
							return w.id;
						});

						t.t(hasid);

						var hasattr1 = dijit.registry.every(function(w){
							return w.attr1 > 0;
						});

						t.t(hasattr1);

						var x = 0;
						var once = dijit.registry.every(function(w){
							x++;
							return false;
						});
						t.is(1, x);
						t.f(once);

						a.destroy();
						b.destroy();

					},
					function someTest(t){

						var a = new Baz();
						var b = new Baz();

						var x = 0;
						var hasid = dijit.registry.some(function(w){
							x++;
							return w.id;
						});

						t.is(1, x);
						t.t(hasid);

						var n = 0;
						var hasattr1 = dijit.registry.some(function(w){
							n++;
							return false;
						});
						t.is(n, dijit.registry.length);
						t.f(hasattr1);

						var l = 0;
						var once = dijit.registry.some(function(w){
							l++;
							return true;
						});
						t.is(1, l);
						t.t(once);

						a.destroy();
						b.destroy();

					},
					function deleteTest(t){
						var names=[];
						dijit.byId("two").destroy();
						dijit.byId("four").destroy();
						dijit.registry.forEach(function(widget){ names.push(widget.name); });
						t.is(names.join(" "), "bob your");
					},
					function getEnclosingWidgetTest(t){
						t.is(dijit.getEnclosingWidget(dojo.byId("not-a-widget")), null);
						t.is(dijit.getEnclosingWidget(dojo.byId("three")).name, "your");
						t.is(dijit.getEnclosingWidget(dojo.byId("three.one")).name, "your");
						t.is(dijit.getEnclosingWidget(dojo.byId("three.one.one")).name, "your");
					}
				]
			);

			doh.run();
		});

	</script>
</head>
<body>
	<h1>Dijit Manager Unit Test</h1>
	<div id="one" data-dojo-type="foo" data-dojo-props='name:"bob", attr1:10, attr2:10'></div>
	<div id="two" data-dojo-type="foo" data-dojo-props='name:"is", attr1:5, attr2:10'></div>
	<div id="three" data-dojo-type="bar" data-dojo-props='name:"your", attr1:5, attr2:5'>
		<div id="three.one">
			<div id="three.one.one"></div>
		</div>
	</div>
	<div id="four" data-dojo-type="bar" data-dojo-props='name:"uncle", attr1:10, attr2:5'></div>
	<div id="not-a-widget"></div>
</body>
</html>
